<?php

class TrxPattycashController extends Controller
{
       

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new TrxPattycash;
		$sql = " select coalesce(a.saldo,0) + b.nominal - c.nominal + d.nominal - e.nominal as saldo
 from mst_coa a
 inner join (select coalesce(sum(nominal),0) as nominal from trx_voucher where coa_debet_id = 10) b on 1=1
 inner join (select coalesce(sum(nominal),0) as nominal from trx_pattycash) c on 1=1
  inner join (select coalesce(sum(debet),0) as nominal from hist_jurnal where mst_account_id= 10 ) d on 1=1
   inner join (select coalesce(sum(credit),0) as nominal from hist_jurnal where mst_account_id= 10 ) e on 1=1
where code='10A200.01'
  ";
		$command = Yii::app()->db->createCommand($sql);
		$datas = $command->queryRow();
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['TrxPattycash']))
		{
			$model->attributes=$_POST['TrxPattycash'];
			$tgl_transaksi = $_POST['tgl_transaksi'];
			$jns_trx = $_POST['jns_trx'];
			$nominal = $_POST['nominal'];
			$keterangan = $_POST['keterangan'];
			for($i=0;$i<count($nota_note);$i++)
			{
				
				if($nota_note[$i]!=""){
					$model=new TrxPattycash;
					if($i != 0){
						$model->no_voucher = $nomor;
					}
					$model->tgl_transaksi = date('Y-m-d',strtotime($_POST['TrxPattycash']['tgl_transaksi']));
					$model->jns_trx = $jns_trx[$i];
					$model->nota_note = $nota_note[$i];
					$model->nominal = trim($nominal[$i]);
					$model->keterangan = $keterangan[$i];
					$model->save();
					if($i==0){
						$nomor = "PC-".date("Ymd").$model->id;
						TrxPattycash::model()->updateByPk($model->id,array('no_voucher'=>$nomor));
					}
					
				}
				
				
			}
				$this->redirect(array('admin'));
		}

		$this->render('create',array(
			'model'=>$model,
			'saldo'=>number_format($datas['saldo']),
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['TrxPattycash']))
		{
			$model->attributes=$_POST['TrxPattycash'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
            $session=new CHttpSession;
            $session->open();		
            $criteria = new CDbCriteria();            

                $model=new TrxPattycash('search');
                $model->unsetAttributes();  // clear any default values

                if(isset($_GET['TrxPattycash']))
		{
                        $model->attributes=$_GET['TrxPattycash'];
			
			
                   	
                       if (!empty($model->id)) $criteria->addCondition('id = "'.$model->id.'"');
                     
                    	
                       if (!empty($model->nota_note)) $criteria->addCondition('nota_note = "'.$model->nota_note.'"');
                     
                    	
                       if (!empty($model->coa_debet_id)) $criteria->addCondition('coa_debet_id = "'.$model->coa_debet_id.'"');
                     
                    	
                       if (!empty($model->coa_credit_id)) $criteria->addCondition('coa_credit_id = "'.$model->coa_credit_id.'"');
                     
                    	
                       if (!empty($model->nominal)) $criteria->addCondition('nominal = "'.$model->nominal.'"');
                     
                    	
                       if (!empty($model->keterangan)) $criteria->addCondition('keterangan = "'.$model->keterangan.'"');
                     
                    	
                       if (!empty($model->createdby)) $criteria->addCondition('createdby = "'.$model->createdby.'"');
                     
                    	
                       if (!empty($model->createdtime)) $criteria->addCondition('createdtime = "'.$model->createdtime.'"');
                     
                    	
                       if (!empty($model->updatedby)) $criteria->addCondition('updatedby = "'.$model->updatedby.'"');
                     
                    	
                       if (!empty($model->updatedtime)) $criteria->addCondition('updatedtime = "'.$model->updatedtime.'"');
                     
                    	
                       if (!empty($model->tgl_transaksi)) $criteria->addCondition('tgl_transaksi = "'.$model->tgl_transaksi.'"');
                     
                    	
                       if (!empty($model->client_name)) $criteria->addCondition('client_name = "'.$model->client_name.'"');
                     
                    	
                       if (!empty($model->mst_client_id)) $criteria->addCondition('mst_client_id = "'.$model->mst_client_id.'"');
                     
                    	
                       if (!empty($model->sisa_bayar)) $criteria->addCondition('sisa_bayar = "'.$model->sisa_bayar.'"');
                     
                    	
                       if (!empty($model->kurang_bayar)) $criteria->addCondition('kurang_bayar = "'.$model->kurang_bayar.'"');
                     
                    	
                       if (!empty($model->no_nota_id)) $criteria->addCondition('no_nota_id = "'.$model->no_nota_id.'"');
                     
                    	
                       if (!empty($model->jns_trx)) $criteria->addCondition('jns_trx = "'.$model->jns_trx.'"');
                     
                    	
                       if (!empty($model->no_voucher)) $criteria->addCondition('no_voucher = "'.$model->no_voucher.'"');
                     
                    	
                       if (!empty($model->jns_mata_uang)) $criteria->addCondition('jns_mata_uang = "'.$model->jns_mata_uang.'"');
                     
                    	
                       if (!empty($model->hg_nh)) $criteria->addCondition('hg_nh = "'.$model->hg_nh.'"');
                     
                    			
		}
                 $session['TrxPattycash_records']=TrxPattycash::model()->findAll($criteria); 
       

                $this->render('index',array(
			'model'=>$model,
		));

	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new TrxPattycash('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['TrxPattycash']))
			$model->attributes=$_GET['TrxPattycash'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=TrxPattycash::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='trx-pattycash-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
        public function actionGenerateExcel()
	{
            $session=new CHttpSession;
            $session->open();		
            
             if(isset($session['TrxPattycash_records']))
               {
                $model=$session['TrxPattycash_records'];
               }
               else
                 $model = TrxPattycash::model()->findAll();

		
		Yii::app()->request->sendFile(date('YmdHis').'.xls',
			$this->renderPartial('excelReport', array(
				'model'=>$model
			), true)
		);
	}
        public function actionGeneratePdf() 
	{
           $session=new CHttpSession;
           $session->open();
		Yii::import('application.modules.admin.extensions.giiplus.bootstrap.*');
		require_once('tcpdf/tcpdf.php');
		require_once('tcpdf/config/lang/eng.php');

             if(isset($session['TrxPattycash_records']))
               {
                $model=$session['TrxPattycash_records'];
               }
               else
                 $model = TrxPattycash::model()->findAll();



		$html = $this->renderPartial('expenseGridtoReport', array(
			'model'=>$model
		), true);
		
		//die($html);
		
		$pdf = new TCPDF();
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor(Yii::app()->name);
		$pdf->SetTitle('TrxPattycash Report');
		$pdf->SetSubject('TrxPattycash Report');
		//$pdf->SetKeywords('example, text, report');
		$pdf->SetHeaderData('', 0, "Report", '');
		$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "Example Report by ".Yii::app()->name, "");
		$pdf->setHeaderFont(Array('helvetica', '', 8));
		$pdf->setFooterFont(Array('helvetica', '', 6));
		$pdf->SetMargins(15, 18, 15);
		$pdf->SetHeaderMargin(5);
		$pdf->SetFooterMargin(10);
		$pdf->SetAutoPageBreak(TRUE, 0);
		$pdf->SetFont('dejavusans', '', 7);
		$pdf->AddPage();
		$pdf->writeHTML($html, true, false, true, false, '');
		$pdf->LastPage();
		$pdf->Output("TrxPattycash_002.pdf", "I");
	}
}
